package com.lindroy.androidplayer.ui.coin

import android.os.Bundle
import android.view.View
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.base.BaseListFragment
import com.lindroy.androidplayer.databinding.FragmentCoinRankingBinding
import com.lindroy.androidplayer.ui.adapter.CoinRankingAdapter
import com.lindroy.androidplayer.utils.addSimpleDivider
import com.lindroy.androidplayer.utils.collectListWhenResumed
import com.lindroy.androidplayer.viewmodels.CoinRankingViewModel

/**
 * @author Lin
 * @date 2022/2/14
 * @function 积分排行榜
 */
class CoinRankingFragment : BaseListFragment<FragmentCoinRankingBinding, CoinRankingViewModel>() {

    private val adapter by lazy { CoinRankingAdapter(viewLifecycleOwner) }

    override fun FragmentCoinRankingBinding.initView(savedInstanceState: Bundle?) {
        vm = mViewMode
        initToolbar(R.string.coin_ranking)
        rvCoinRanking.addSimpleDivider()
        rvCoinRanking.adapter = adapter.withFooter()
    }

    override fun FragmentCoinRankingBinding.initListener() {
        mViewMode.setLayoutStateFlow(adapter.loadStateFlow)
        mViewMode.coinRankingList.collectListWhenResumed(viewLifecycleOwner) {
            adapter.submitData(it)
        }
    }

    override fun onRefresh() {
        adapter.refresh()
    }

    override fun onRetry(view: View) {
        adapter.refresh()
    }
}